import service from '../../services/choose';
import base from '../../config/base.js'
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon_top_bg: base.static + 'choose/mp-haiwai-bg.png',
    icon_overseas: base.static + 'detail/haigou.png',
    goodsList: [],
    loadBloon: true,
    endBloon: true,
    pages: 0,
    nomore: true,
    user_id: '',
    store_id: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let user_info = wx.getStorageSync('user_info') || {id:'',lastView:{seid:''}};
    that.setData({
      user_id: user_info.id,
      store_id: user_info.lastView.seid,
    });
    that.queryList(1)
  },
  // 商品列表
  queryList: function (page){
    let that = this;
    let params = {
      page: page,
      offset: 10
    };
    service.getZyGoodsList(params, function (res) {
      if (res && res.code == 1) {
        let stolistAll = that.data.goodsList.concat(res.data.list);
        try{
          stolistAll.forEach((item, index) => {
            item.goods_id = item.id
            item.src = item.goods_pic.url
          })
        }catch(e){}
        that.setData({
          goodsList: stolistAll,
          pages: page + 1,
          endBloon: true
        });
        if (stolistAll.length >= res.data.count) {
          that.setData({
            loadBloon: false,
            nomore: false,
          })
        }
      }
      wx.stopPullDownRefresh();
    });
  },
  //监听页面高度
  scrollTimer: null,
  onPageScroll: function (res) {
    let that = this;
    if (that.scrollTimer) { clearTimeout(that.scrollTimer)};
    that.scrollTimer = setTimeout(() => {
      that.setData({
        scrolltop: res.scrollTop
      })
    }, 500);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
  onPullDownRefresh: function () {
    let that = this;
    that.setData({
      goodsList: [],
      loadBloon: true,
      endBloon: true,
      nomore: true,
    })
    that.queryList(1);

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    if (that.data.loadBloon && that.data.endBloon) {
      that.setData({
        endBloon: false
      })
      that.queryList(that.data.pages);
    }
  },
})